package com.jinly.ryder.deploy.web;

import com.jinly.ryder.common.auth.annotation.Auth;
import com.jinly.ryder.common.auth.annotation.AuthParam;
import com.jinly.ryder.common.auth.model.AuthRole;
import com.jinly.ryder.common.auth.model.AuthType;
import com.jinly.ryder.deploy.service.DeployRecordService;
import com.jinly.ryder.deploy.service.model.DeployProgress;
import com.jinly.ryder.deploy.web.vo.DeployInstanceStatus;
import com.jinly.ryder.manage.service.AppInfoService;
import com.jinly.ryder.manage.service.AppInstanceService;
import com.jinly.ryder.manage.service.model.AppInfo;
import com.jinly.ryder.manage.service.model.AppInstance;
import com.jinly.ryder.manage.service.model.AppMonitorType;
import com.jinly.ryder.manage.service.model.DeployStatus;
import com.jinly.ryder.monitor.service.ActuatorQueryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("deployStatus")
@RequiredArgsConstructor
public class DeployStatusController {

    private final AppInfoService appInfoService;
    private final AppInstanceService appInstanceService;
    private final DeployRecordService deployRecordService;
    private final ActuatorQueryService actuatorQueryService;

    @GetMapping("list")
    @Auth(type = AuthType.APP, role = AuthRole.DEVELOPER, value = "#{appName}")
    public List<DeployInstanceStatus> groupList(@AuthParam("appName") String appName, String groupName) {
        List<AppInstance> list = appInstanceService.list(appName, groupName);
        AppInfo appInfo = appInfoService.getAppInfo(appName);
        return list.parallelStream().map(ins -> createInstanceStatus(appInfo, ins)).collect(Collectors.toList());
    }

    private DeployInstanceStatus createInstanceStatus(AppInfo appInfo, AppInstance appInstance) {
        DeployInstanceStatus deployInstanceStatus = new DeployInstanceStatus();
        deployInstanceStatus.setInstanceId(appInstance.getId());
        deployInstanceStatus.setDeployStatus(appInstance.getDeployStatus());
        if (appInfo.getMonitorType() == AppMonitorType.SPRING_ACTUATOR) {
            if (appInfo.getMonitorSetting().isServiceRegistry()) {
                deployInstanceStatus.setServiceStatus(actuatorQueryService.serviceRegistry(appInstance.getHost(), appInstance.getPort()));
            }
            deployInstanceStatus.setHostStatus(actuatorQueryService.health(appInstance.getHost(), appInstance.getPort()));
        }
        if (appInstance.getDeployStatus() == DeployStatus.DEPLOYING) {
            DeployProgress deployProgress = deployRecordService.getDeployProgress(appInstance.getHost(), appInstance.getPort());
            if (deployProgress != null) {
                deployInstanceStatus.setDeployingTaskName(deployProgress.getTaskName());
                deployInstanceStatus.setDeployingProgress(deployProgress.getProgress());
            }
        }
        return deployInstanceStatus;
    }
}
